package com.msoe.client.panels;

import org.zenika.widget.client.datePicker.DatePicker;

import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.MapTypeControl;
import com.google.gwt.maps.client.control.SmallMapControl;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geocode.DirectionQueryOptions;
import com.google.gwt.maps.client.geocode.DirectionResults;
import com.google.gwt.maps.client.geocode.Directions;
import com.google.gwt.maps.client.geocode.DirectionsCallback;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.msoe.client.Session;
import com.msoe.client.customwidgets.NavigationMenu;

public class GetRide implements MSOERPanel, ClickListener{

	public static final String DIV_ID = "get_ride";
	private LatLng DEFAULT_CENTER_LATLNG = LatLng.newInstance(43.181648,-89.24572);
	private String MSOE_DEFAULT_LOCATION = "1121 North Milwaukee Street Milwaukee WI, 53202";
	private int DEFAULT_ZOOM_LEVEL = 7;

	private Panel GetRidePanel;
	private Session session;
	private MapWidget mapWiget;
	DatePicker datePicker;
	private MarkerClick markerClickHandler;

	public GetRide(Session session){
		this.session = session;

		GetRidePanel = new VerticalPanel();
		markerClickHandler = new MarkerClick();

		HorizontalPanel ButtonPanel = new HorizontalPanel();
		HorizontalPanel DatePanel = new HorizontalPanel();
		HorizontalPanel MapPanel = new HorizontalPanel();
		NavigationMenu navMenu = new NavigationMenu(this);

		Label L_Date = new Label("Please select a date");
		Button B_ShowRide = new Button("Show Rides");
		datePicker = new DatePicker();

		B_ShowRide.addClickListener(this);

		DatePanel.add(L_Date);
		DatePanel.add(datePicker);
		DatePanel.add(B_ShowRide);

		mapWiget = new MapWidget(DEFAULT_CENTER_LATLNG,DEFAULT_ZOOM_LEVEL);  
		mapWiget.setSize("1000px", "600px");  

		mapWiget.addControl(new SmallMapControl());  
		mapWiget.addControl(new MapTypeControl());  

		//set default zoom level
		//DEFAULT_ZOOM_LEVEL = mapWiget.getZoomLevel();

		MapPanel.add(mapWiget);

		GetRidePanel.add(navMenu);
		GetRidePanel.add(ButtonPanel);
		GetRidePanel.add(DatePanel);
		GetRidePanel.add(MapPanel);
	}

	private void clear(){
		datePicker.setText("");
		mapWiget.clearOverlays();
		mapWiget.setCenter(DEFAULT_CENTER_LATLNG);
		mapWiget.setZoomLevel(DEFAULT_ZOOM_LEVEL);
	}
	
	private void clearOverlaysAndZoom(){
		mapWiget.clearOverlays();
		mapWiget.setCenter(DEFAULT_CENTER_LATLNG);
		mapWiget.setZoomLevel(DEFAULT_ZOOM_LEVEL);
	}


	public void show(){
		if(session.validate()){
			clear();
			RootPanel.get().clear();
			RootPanel.get(DIV_ID).add(GetRidePanel);
		}else{
			PanelMap.getPanel("Login").show();
		}
	}

	public void hide(){
		GetRidePanel.removeFromParent();
	}


	public void onClick(Widget sender) {
		if(sender instanceof Button){
			
			Button clickedButton = ((Button) sender);
			
			if(clickedButton.getText().equals("Show Rides")){

				//clear overlays from any previous call
				clearOverlaysAndZoom();

				//create the query option so that the route overlay is not
				//shown on default but the polyline is still retrieved
				DirectionQueryOptions query = new DirectionQueryOptions();
				query.setRetrievePolyline(true);
				
				DirectionsCallBackHandler directionsHandler = new DirectionsCallBackHandler();
				
				//get the chosen date
				String date = datePicker.getText();
				
				//do RPC call here
				
				//List<> rides = new ArrayList<>();
				
				//for each ride do
				Directions.load("from " + MSOE_DEFAULT_LOCATION + " to Janesville, WI",
						query,
						directionsHandler);

			}
		}
	}

	public Session getSession() {
		return session;
	}

	private class DirectionsCallBackHandler implements DirectionsCallback {

		public void onFailure(int statusCode) {

			MessageBox.showMessageDialog("Service Not Available"
					,"The service is not available, try again later.");
		}

		public void onSuccess(DirectionResults result) {
			
			//get the destination marker
			Marker dest = result.getMarkers().get(result.getMarkers().size() - 1);
			
			//create new marker at destination marker place with correct handler
			Marker destMark = new Marker(dest.getLatLng());
			destMark.addMarkerClickHandler(markerClickHandler);
			
			//add the new marker
			mapWiget.addOverlay(destMark);
			
			//add the route overlay
			mapWiget.addOverlay(result.getPolyline());

		}
	}
	
	private class MarkerClick implements MarkerClickHandler{

		/**
		 * handles the click of a marker on the getRide
		 * page
		 */
		public void onClick(MarkerClickEvent event) {
			
				//when the marker is clicked open a new info window above it with
				//the information about the ride
				mapWiget.getInfoWindow().open(event.getSender().getLatLng(),getRideInfoWindowContent());
			
		}
		
		/**
		 * gets the info for a ride and puts it in a InfoWindowContent
		 * object so it can be displayed in an InfoWindow above a marker
		 * @return
		 */
		private InfoWindowContent getRideInfoWindowContent(){
			
			VerticalPanel vPanel = new VerticalPanel();
			HorizontalPanel hPanel = new HorizontalPanel();
			
			Label info = new Label("Info goes here");
			hPanel.add(info);
			vPanel.add(hPanel);
			
			return new InfoWindowContent(vPanel);
		}
	
	}
	
}
